﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DacSanMienTayOBJ
{
    public class AdvPositionBusiness
    {
        public static bool InsertAdvPosition(AdvPosition adv,ConnectionType typeConnect)
        {
            Sqlhelper helper=new Sqlhelper(typeConnect);
            try
            {
                bool rs=false;
                string sql="insert into advertisingposition(AdvName,IsVisible) values(@advName,@isVisible)";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@advName", adv.AdvPoName);
                pa[1] = new SqlParameter("@isVisible",adv.IsVisible);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch(Exception ex)
            {
                Utilitys.WriteLog("exception insert advPosition " +ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool UpdateAdvPosition(int advID,string advName, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "update advertisingposition set AdvName=@advName where AdvPositionID=@advID";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@advName",advName);
                pa[1] = new SqlParameter("@advID", advID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("exception update advPosition " + ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool UpdateVisibleAdvPosition(int advID, int isVisible, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "update advertisingposition set IsVisible=@isVisible where AdvPositionID=@advID";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@isVisible", isVisible);
                pa[1] = new SqlParameter("@advID", advID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("exception update visible advPosition " + ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<AdvPosition> ListAllAdvPosition( ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<AdvPosition> list = new List<AdvPosition>();
                string sql = "select * from advertisingposition";
               
                SqlCommand command = helper.GetCommandNonParameter(sql, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    AdvPosition adv = new AdvPosition();
                    adv.AdvPositionID = int.Parse(reader["AdvPositionID"].ToString());
                    adv.AdvPoName = reader["AdvName"].ToString();
                    adv.IsVisible = int.Parse(reader["IsVisible"].ToString());
                    list.Add(adv);
                }
               return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("exception List advPosition " + ex.Message);
                return null ;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<AdvPosition> ListAllAdvPositionVisible(ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<AdvPosition> list = new List<AdvPosition>();
                string sql = "select * from advertisingposition where IsVisible=1";

                SqlCommand command = helper.GetCommandNonParameter(sql, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    AdvPosition adv = new AdvPosition();
                    adv.AdvPositionID = int.Parse(reader["AdvPositionID"].ToString());
                    adv.AdvPoName = reader["AdvName"].ToString();
                    adv.IsVisible = int.Parse(reader["IsVisible"].ToString());
                    list.Add(adv);
                }
                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("exception List advPosition Visible " + ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }
    }
}
